package com.cvit.service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cvit.dao.AdmissionMapper;
import com.cvit.model.Admission;
import com.cvit.model.ExcelBean;
import com.cvit.utils.ExcelUtil;
import com.cvit.utils.Utils;

@Service
public class AdmissionService {
	
		@Autowired
		AdmissionMapper admissionMapper;
		
		/**
		 * 查询录取表
		 * */
		public List<Admission> getAdmission() {
			// TODO Auto-generated method stub
			List<Admission> admission = admissionMapper.selectAll(null);
			return admission;
		}

		/**
		 * 根据ID、考生号、身份证号、名字查询
		 * */
		public Admission getAdmissionBy(Admission admission) {
			Admission admissions = admissionMapper.selectAdmissionBy(admission);
			return admissions;
		}
		
		/**
		 * 根据学生号查询 （boolean）
		 * */
		public boolean getAdmissionByExamineeId(String ExamineeId){
			Admission admission = admissionMapper.selectByExamineeId(ExamineeId);
			return Utils.isNull(admission);
		}
		
		/**
		 * 导入Excel
		 * */
		public void importExcelInfo(InputStream in, MultipartFile file) throws Exception{
		    List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,file.getOriginalFilename()); 
		    //遍历listob数据，把数据放到List中
		    for (int i = 0; i < listob.size(); i++) {
		        List<Object> ob = listob.get(i);
		        Admission admission = new Admission();
		        //设置编号  
		        //通过遍历实现把每一列封装成一个model中，再把所有的model用List集合装载
		        admission.setId(JudgeIdRepeat());
		        admission.setExamineeId(String.valueOf(ob.get(0)));
		        admission.setCard(String.valueOf(ob.get(1)));
		        admission.setName(String.valueOf(ob.get(2)));
		        admission.setGender(String.valueOf(ob.get(3)));
		        admission.setProvince(String.valueOf(ob.get(4)));
		        admission.setMajor(String.valueOf(ob.get(5)));
		        admission.setFaculty(String.valueOf(ob.get(6)));
		        admission.setExamineeType(String.valueOf(ob.get(7)));
		        boolean admissionId = getAdmissionByExamineeId(String.valueOf(ob.get(0)));
		        if(admissionId==false){
		        	admissionMapper.deleteByExamineeId(admission.getExamineeId());
		        	}
		        	admissionMapper.insert(admission);
		    }  
		}
		
		/**
		 * 导出Excel
		 * */
		public XSSFWorkbook exportExcelInfo(String admissionData) throws InvocationTargetException, ClassNotFoundException, IntrospectionException, ParseException, IllegalAccessException {  
		    //根据条件查询数据，把数据装载到一个list中  
		    List<Admission> list = admissionMapper.selectAll(null);
		    List<ExcelBean> excel=new ArrayList<>();
		    Map<Integer,List<ExcelBean>> map=new LinkedHashMap<>();
		    XSSFWorkbook xssfWorkbook=null;
		    //设置标题栏
		    excel.add(new ExcelBean("考生号","examineeId",0));
		    excel.add(new ExcelBean("身份证号","card",0));
		    excel.add(new ExcelBean("姓名","name",0));
		    excel.add(new ExcelBean("性别","gender",0));
		    excel.add(new ExcelBean("省份","province",0));
		    excel.add(new ExcelBean("录取专业","major",0));
		    excel.add(new ExcelBean("院系","faculty",0));
		    excel.add(new ExcelBean("考生类别","examineeType",0));
		    excel.add(new ExcelBean("上传时间","startTime",0));
		    map.put(0, excel);
		    String sheetName = admissionData;
		    //调用ExcelUtil的方法
		    xssfWorkbook = ExcelUtil.createExcelFile(Admission.class, list, map, sheetName);
		    return xssfWorkbook;
		}
		
		/**
		 * Id判重
		 * */
		public String JudgeIdRepeat(){
			String id=null;
			int i=0;
			for(;i==0;){
				String orderNo = "04";
				SimpleDateFormat sdf=new SimpleDateFormat("yyMMdd");
				String nowTime = sdf.format(new Date())+Utils.get6Radom();
				orderNo+=nowTime;
				id = orderNo;
				if(admissionMapper.selectByPrimaryKey(id)==null){
					i=1;
				}
			}
			return id;
		}

		/**
		 * 根据身份证查询
		 * */
		public Admission getAdmissionByCardId(String cardId) {
			// TODO Auto-generated method stub
			return admissionMapper.selectByCardId(cardId);
		}

}
